package com.gitee.cui.netty.encoder;

import com.gitee.cui.util.Logger;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test;

/**
 * 整型转换为ByteBuf编码器测试类
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/10/25 10:15
 **/
public class Integer2ByteEncoderTests {

    @Test
    public void integer2ByteEncoderTest() {
        ChannelInitializer<EmbeddedChannel> initializer = new ChannelInitializer<EmbeddedChannel>() {
            @Override
            protected void initChannel(EmbeddedChannel ch) throws Exception {
                ch.pipeline().addLast(new Integer2ByteEncoder());
            }
        };

        EmbeddedChannel channel = new EmbeddedChannel(initializer);
        for (int i = 0; i < 100; i++) {
            // 向通道中写入整数
            channel.write(i);
        }
        channel.flush();
        ByteBuf buf = channel.readOutbound();
        while (buf != null) {
            Logger.info(buf.readInt());
            buf = channel.readOutbound();
        }
    }
}
